/*
注:以下的整型数最低位为bit0.
(1)随机给定一个16位无符号整型数a的值.然后,设置a的bit3为1,保持其他位不变.
(2)随机给定一个16位无符号整型数b的值.然后,设置b的bit3和bit7为0,保持其他位不变.
(3)随机给定一个16位无符号整型数var1的值.然后,设置var1的bit4-bit13为1,保持其他位不变.
(4)随机给定一个32位无符号整型数var2的值.然后取出var2的bit3-bit8.
(5)随机给定一个32位无符号整型数var3的值.快速将其除以8并打印出结果.
(6)随机给定一个32位无符号整型数var4的值.快速将其乘以2并打印出结果.
*/
#include <stdio.h>
#include "../MyFunc/MyFunc.c" //包含自定义的函数
int main(void)
{
	//    (1)
	unsigned short int a = 0;
	a = rand() * 65535;
	printf("\n随机a的二进制数为:");
//	myFuncToBin(a);
	myFuncUint16ToBin(a);
	a |= 1 << 3;
	printf("\n置位后,a的二进制数为:");
	myFuncUint16ToBin(a);	
//    myFuncToBin(a);
//	char str1[16] = "";
//	myFuncToBin2(a, str1);
//	printf("\n");
//	for (int i = 15; i >= 0; i--)
//	{
//		if (str1[i] == '\0')
//			putchar('0');
//		else
//			putchar(str1[i]);
//	}

	//    (2)
	unsigned short int b = 0;
	b = rand() * 65535;
	printf("\n\n随机b的二进制数为:");
	myFuncToBin(b);
	myFuncUint16ToBin(b);
	b = b & ~(1 << 3); // set bit3 to 0
	b = b & ~(1 << 7); // set bit7 to 0
	printf("\n置位后,b的二进制数为:");
//    myFuncToBin(b);
myFuncUint16ToBin(b);
	char str2[16] = "";
	myFuncToBin2(b, str2);
	printf("\n");
	for (int i = 15; i >= 0; i--)
	{
		if (str2[i] == '\0')
			putchar('0');
		else
			putchar(str2[i]);
	}
	//    (3)
	unsigned short int var1 = 0;
	var1 = rand() * 65535;
	printf("\n\n随机var1的二进制数为:");
	myFuncToBin(var1);
	var1 = var1 | (0x1FF << 4); // set bit4-bit13 to 1
	printf("\n置位后,var1的二进制数为:");
//    myFuncToBin(var1);
	char str3[16] = "";
	myFuncToBin2(var1, str3);
	printf("\n");
	for (int i = 15; i >= 0; i--)
	{
		if (str3[i] == '\0')
			putchar('0');
		else
			putchar(str3[i]);
	}

	//    (4)
	unsigned int var2 = 0;
	var2 = rand() * 65535 * 65535;
	printf("\n\n随机var2的二进制数为:");
	myFuncToBin(var2);
	var2 = var2 & (0x3F << 3); //取出var2的bit3-bit8
	var2 = var2 >> 3;
	printf("\n\n取出的数的二进制数为:");
//    myFuncToBin(var2);
	char	str4[32] = "";
	myFuncToBin2(var2, str4);
	printf("\n");
	for (int i = 31; i >= 0; i--)
	{
		if (str4[i] == '\0')
			putchar('0');
		else
			putchar(str4[i]);
	}

	//    (5)
	unsigned int var3 = 0;
	var3 = rand() * 65535 * 65535;
	printf("\n\n随机var3的二进制数为:");
	myFuncToBin(var3);
	var3 = var3 >> 3; //快速除以8
	printf("\n快速除以8后所得二进制数为:");
//    myFuncToBin(var3);
	char str5[32] = "";
	myFuncToBin2(var3, str5);
	printf("\n");
	for (int i = 31; i >= 0; i--)
	{
		if (str5[i] == '\0')
			putchar('0');
		else
			putchar(str5[i]);
	}

	//    (6)
	unsigned int var4 = 0;
	var4 = rand() * 65535;
	printf("\n\n随机var4的二进制数为:");
	myFuncToBin(var4);
	var4 = var4 << 2; //快速乘以2
	printf("\n快速乘以后所得二进制数为:");
//    myFuncToBin(var4);
	char str6[32] = "";
	myFuncToBin2(var4, str6);
	printf("\n");
	for (int i = 31; i >= 0; i--)
	{
		if (str6[i] == '\0')
			putchar('0');
		else
			putchar(str6[i]);
	}

}
